//
//  PYSandbox.m
//  catuncel
//
//  Created by Jim on 2017/10/6.
//  Copyright © 2017年 Jim. All rights reserved.
//

#import "PYSandbox.h"

#import "NSDate+JZCalendarLogic.h"

#import <AVFoundation/AVFoundation.h>

@implementation PYSandbox

+ (NSString *)saveImage:(UIImage *)image
{
    // 设置图片名称
    NSString *imageName = [NSString stringWithFormat:@"%f.png", [[[NSDate date] localDate] timeIntervalSince1970]];
    
    // 设置图片存储路径
    NSString *path = [NSString stringWithFormat:@"/Documents/chatImages/%@", imageName];
    
    // 拼接图片的沙盒路径
    NSString *imagePath = [NSHomeDirectory() stringByAppendingString:path];
    
    // 存储图片到沙盒
    NSData *data;
    //判断图片是不是png格式的文件
    if (UIImagePNGRepresentation(image)) {
        //返回为png图像。
        data = UIImagePNGRepresentation(image);
    } else {
        //返回为JPEG图像。
        data = UIImageJPEGRepresentation(image, 0.1);
    }
    [data writeToFile:imagePath atomically:YES];
    
    return imagePath;
}

+ (UIImage *)getImageWithPath:(NSString *)path
{
    // 拿到沙盒路径图片
    return [[UIImage alloc] initWithContentsOfFile:path];
}

/**
 *  获取视频的缩略图方法
 *
 *  @param filePath 视频的本地路径
 *
 *  @return 视频截图
 */
- (UIImage *)getScreenShotImageFromVideoPath:(NSString *)filePath{
    
    UIImage *shotImage;
    //视频路径URL
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL options:nil];
    
    AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
    gen.appliesPreferredTrackTransform = YES;
    
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    
    NSError *error = nil;
    
    CMTime actualTime;
    
    CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    
    shotImage = [[UIImage alloc] initWithCGImage:image];
    
    CGImageRelease(image);
    
    return shotImage;
    
}

@end





























